#include <stdio.h>
#include <stdlib.h>
#include "stackADT.h"

char mainChoice();
ElementType getInputNumberToElementType();
void putElement(ElementType *elem);

int main()
{
    SeqStack mainStack;
    int initializeSize = 15;
    InitializeStack(&mainStack, initializeSize);
    printf("Initialize Stack  with %d space Success!\n", initializeSize);

    char choice;
    do
    {
        choice = mainChoice();
        switch (choice)
        {
        case '1':
            puts("1. PushStack");
            ElementType elem = getInputNumberToElementType();
            // stack中是解引用后赋值到malloc开辟的数组中,这里不用考虑ElementType生存周期的问题
            printf(PushStack(&mainStack, &elem) ? "Success!\n" : "false\n");
            break;
        case '2':
            puts("2. PopStack");
            ElementType *elem1;
            if (PopStack(&mainStack, elem1))
            {
                puts("Success!");
                putElement(elem1);
            }
            else
                puts("false");
            break;
        case '3':
            puts("3. StackItemCount");
            printf("StackItemCount is %d\n", StackItemCount(&mainStack));
            break;
        case '4':
            puts("4. GetTopOfStack");
            ElementType *elem2;
            elem2 = GetTopOfStack(&mainStack);
            putElement(elem2);
            break;
        case '5':
            puts("5. ListStack");
            ListStack(&mainStack);
            break;
        case '6':
            puts("6. StackIsEmpty");
            printf(StackIsEmpty(&mainStack) ? "true!\n" : "false\n");
            break;
        case '7':
            puts("7. StackIsFull");
            printf(StackIsFull(&mainStack) ? "true!\n" : "false\n");
            break;
        case '8':
            puts("8. EmptyStack");
            printf(EmptyStack(&mainStack) ? "Success!\n" : "false\n");
            break;
        case 'q':
            puts("cleaning.....");
            printf(EmptyStack(&mainStack) ? "EmptyStack Success\n" : "false\n");
            puts("exiting.....");
            exit(0);
            break;

        default:
            puts("Default\n");
            break;
        }
    } while (choice != 'q');
    return 0;
}

char mainChoice()
{
    char choice;
    puts("---------------------------------------");
    puts("1. PushStack");
    puts("2. PopStack");
    puts("3. StackItemCount");
    puts("4. GetTopOfStack");
    puts("5. ListStack");
    puts("6. StackIsEmpty");
    puts("7. StackIsFull");
    puts("8. EmptyStack");
    puts("q. EXIT");


    puts("Please input your choice: ");
    while (scanf("%c", &choice))
    {
        while (getchar() != '\n')
            continue;

        printf("choice is %c \n", choice);
        return choice;
    }
}

ElementType getInputNumberToElementType()
{
    ElementType temp;
    puts("Please input your ElementType(int): ");
    while (scanf("%d", &temp))
    {
        while (getchar() != '\n')
            continue;

        return temp;
    }
}

void putElement(ElementType *elem)
{
    printf("Element is %d \n", *elem);
}